﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace KoPortal2.Sys.Metadata
{
    public static class MetadataSyncBase
    {
        public static Exception Insert(String parNameLib, String parID, String parName, String parFullName, int lcid)
        {
            Exception ret = null;
            using (SPSite site = new SPSite("http://localhost"))
            {
                ret = Insert(parNameLib, parID, parName, parFullName, lcid);
            }
            return ret;
        }
        public static Exception Insert(SPSite site, String parNameLib, String parID, String parName, String parFullName, int lcid)
        {
            Exception ret = null;
            try
            {
                TaxonomySession mSess = new TaxonomySession(site);
                foreach (TermStore mStore in mSess.TermStores)
                {
                    foreach (Group mGroup in mStore.Groups)
                    {
                        foreach (TermSet mSet in mGroup.TermSets)
                        {
                            if (mSet.Name == parNameLib)
                            {
                                Term mTerm = mSet.CreateTerm(parName, lcid);
                                mTerm.SetDescription(parID, lcid);
                                mTerm.CreateLabel(parFullName, lcid, false);
                            }
                        }
                    }
                    mStore.CommitAll();
                }
            }
            catch (Exception ex)
            {
                ret = new Exception(ex.Message, ex);
            }
            return ret;
        }
        static public Exception Delete(String parNameLib)
        {
            Exception ret = null;
            using (SPSite site = new SPSite("http://localhost"))
            {
                ret = Delete(site, parNameLib);
            }
            return ret;
        }
        static public Exception Delete(SPSite site, String mNameLib)
        {
            Exception ret = null;
            try
            {
                TaxonomySession mSess = new TaxonomySession(site);
                foreach (TermStore mStore in mSess.TermStores)
                {
                    foreach (Group mGroup in mStore.Groups)
                    {
                        foreach (TermSet mSet in mGroup.TermSets)
                        {
                            if (mSet.Name == mNameLib)
                            {
                                foreach (Term mTerm in mSet.Terms)
                                    mTerm.Delete();
                            }
                        }
                    }
                    mStore.CommitAll();
                }
            }
            catch (Exception ex)
            {
                ret = new Exception(ex.Message, ex);
            }
            return ret;
        }
    }
}
